package gui;

import game.IGameData;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

import domain.league.ILeague;
import domain.match.IGameMatch;
import domain.tournament.ITournament;

import application.ArenaManager;

public class DataList extends JPanel {

	private List<?> dataList;
	private JLabel lblListHeader;
	private int listIndex;
	private JList jList;
	

	public DataList(String title, List<?> dataList) {
		this.dataList = dataList;
		
		createPanel();
		populateList();
		updateTitle(title);
	}
	
	private void populateList() {
		jList.setListData(dataList.toArray());
	}

	private void createPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		
		lblListHeader = new JLabel("");
		lblListHeader.setFont(new Font("Tahoma", Font.BOLD, 13));
		panel.add(lblListHeader);

		jList = new JList();
		
		jList.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				listIndex = jList.locationToIndex(e.getPoint());
				if (e.getClickCount() == 2) {
					// int index = jlist.locationToIndex(e.getPoint());
					if (listIndex >= 0) {
						performItemAction(jList.getModel().getElementAt(listIndex));
					}
				}
			}
		});

		add(jList, BorderLayout.CENTER);

		JPanel splitPanel = new JPanel();
		add(splitPanel, BorderLayout.SOUTH);
		splitPanel.setLayout(new BorderLayout(0, 0));

		JButton btnBack = new JButton("Back");
		splitPanel.add(btnBack, BorderLayout.WEST);

		JButton btnSelect = new JButton("Continue");
		splitPanel.add(btnSelect, BorderLayout.EAST);
		btnSelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				performItemAction(getListObject(listIndex));
			}
		});
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getPreviousList(getListObject(listIndex));
			}
		});

	}
	
	private Object getListObject(int index) {
		try {
			return jList.getModel().getElementAt(listIndex);
		} catch (Exception e) {
			return null;
		}
	}

	private void getPreviousList(Object o) {
		ArenaManager.getInstance().listGames();
	}

	private void performItemAction(Object o) {
		if (o instanceof IGameMatch) {
			((IGameMatch) o).startMatch();
		} else {
			generateList(o);
		}
	}
	
	private void generateList(Object o) {
		String title = "";
		
		if (o instanceof IGameData) {
			title = "Leauges";
			dataList = ((IGameData) o).getLeagues();
		} else if (o instanceof ILeague) {
			title = "Tournaments";
			dataList = ((ILeague) o).getTournaments();
		} else if (o instanceof ITournament) {
			title = "Matches";
			dataList = ((ITournament) o).getMatches();
		}
			
		populateList();
		updateTitle(title);
	}

	private void updateTitle(String title) {
		lblListHeader.setText(title);
	}
	
}
